import 'package:shared_preferences/shared_preferences.dart';

class DatabaseUtil {
  static SharedPreferences? _prefs;

  DatabaseUtil() {
    if (_prefs == null) {
      _buildUtil();
    }
  }

  SharedPreferences? getDriver() {
    return _prefs;
  }

  void _buildUtil() async {
    _prefs = await SharedPreferences.getInstance();
  }

  // 添加值
  static void setString({required String key, required Object value}) {
    DatabaseUtil();
    _prefs?.setString(key, value.toString());
  }

  // 获取值
  static String? getString(String key) {
    DatabaseUtil();
    return _prefs?.getString(key);
  }

  static void setList(String key, List<String> value) {
    DatabaseUtil();
    _prefs?.setStringList(key, value);
  }

  static List<String> getList(String key) {
    DatabaseUtil();
    List<String>? response = _prefs?.getStringList(key);
    response ??= [];
    return response;
  }

  // 删除
  static void remove(String key) {
    DatabaseUtil();
    _prefs?.remove(key);
  }

  // 获取所有的key
  static List<String> getAllKey() {
    DatabaseUtil();
    List<String> response = _prefs?.getKeys().toList() ?? [];
    return response;
  }
}
